﻿namespace KinonekoSoftware.CodariaDB.Documents.Keywords
{
    [Impression]
    public sealed class KeywordEngine : _Full<Keyword>
    {
        public override string               GetCollectionName()       => CollectionNames.KeywordDB;
        public          IEnumerable<Keyword> GetKeywords(string  id)   => InsideDB.Find(x => x.DocumentID == id);
        public          IEnumerable<Keyword> GetDocuments(string name) => InsideDB.Find(x => x.Name == name);
        
        public void AddKeyword(Keyword keyword)
        {
            if (string.IsNullOrEmpty(keyword?.Id))
            {
                return;
            }

            InsideDB.Insert(keyword);
        }
        
        public void RemoveKeyword(Keyword keyword)
        {
            
            if (string.IsNullOrEmpty(keyword?.Id))
            {
                return;
            }

            InsideDB.Delete(keyword.Id);
        }
        
        public void RemoveKeyword(IEnumerable<Keyword> keywords)
        {
            if (keywords is null)
            {
                return;
            }

            var set = keywords.Select(x => x.Id).ToHashSet();
            InsideDB.DeleteMany(x => set.Contains(x.Id));
        }
        
        public void RemoveKeyword(string id)
        {
            
            if (string.IsNullOrEmpty(id))
            {
                return;
            }

            //
            //
            var removeItems = InsideDB.Find(x => x.DocumentID == id).Select(x => x.Id).ToHashSet();

            //
            //
            InsideDB.DeleteMany(x => removeItems.Contains(x.DocumentID));
        }

        public override string EngineID => EngineNames.Keyword;

    }
}